// gjw/pages/dingzhi/post.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgs: [],
    items: [{
        name: 'yes',
        value: '推荐',
        checked: true
      },
      {
        name: 'no',
        value: '不推荐',
      },
    ],
    setTop: [{
        name: 'yes',
        value: '置顶',
        checked: true
      },
      {
        name: 'no',
        value: '不置顶',
      },
    ],
    checked: true,
    upstate: 1,
    store_identity: false//定制商家身份
  },

  bindPickerChange: function(e) {
    var that = this,
      a = that.data
    var index = e.detail.value
    var value = a.array[index]
    var nav = a.nav
    for (let i in nav) {
      if (nav[i].cate == value) {
        that.setData({
          index: e.detail.value,
          nav_id: nav[i].id
        })
      }
    }
  },
  chooseImage: function(e) {
    var that = this
    var imgs = that.data.imgs
    wx.chooseImage({
      count: 9 - imgs.length, // 默认9
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        
        var tempFilePaths = res.tempFilePaths
        imgs = imgs.concat(tempFilePaths)
        that.setData({
          imgs: imgs
        })
      }
    })
  },

  delete_img: function (e) {
    var that = this
    var imgs = that.data.imgs
    var index = e.currentTarget.dataset.index
    imgs.splice(index, 1)
    that.setData({
      imgs: imgs
    })
  },
  uploadimg: function(data) {
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'upload_file',
      formData: null,
      success: (resp) => {
        if (resp.data != '') {
          success++;
          data.path[i] = resp.data
          
        } else {
          wx.showToast({
            icon: "loading",
            title: "请重试"
          })
        }
      },
      fail: (res) => {
        fail++;
      },
      complete: () => {
        i++;
        if (i == data.path.length) {
          
          
          // 图片上传成功之后提交信息
          that.place_order(data.path)
          // that.setData({
          //   imgs: data.path
          // });
        } else {
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }

      }
    });
  },
  radioChange: function(e) {
    if (e.detail.value == 'no') {
      this.setData({
        checked: false,
        upstate: 0
      })
		}else if (e.detail.value == 'yes') {
			this.setData({
				checked: true,
				upstate: 1
			})
		}
  },
  formSubmit: function(e) {
    var that = this
    var a = that.data
    var value = e.detail.value
    if (a.nav_id == null) {
      that.showModel('请选择帖子分类')
    } else if (value.title == '') {
      that.showModel('请输入帖子标题')
    } else if (value.detail == '') {
      that.showModel('请输入帖子内容')
    } else if (a.imgs.length == 0) {
      that.showModel('清上传图片')
    } else {
      that.setData({
        formid: e.detail.formId,
        title: value.title,
        content: value.detail
      })
      // 判断表单输入完成后上传图片
      that.uploadimg({
        url: a.url + 'app/index.php?i=' + a.uniacid + '&c=entry&a=wxapp&do=Uploads&m=zh_gjw',
        path: that.data.imgs
      });
    }
  },
  showModel: function(detail) {
    wx.showModal({
      title: '温馨提示',
      content: detail,
    })
  },
  place_order: function(e) {
			var that = this
			var store_identity
			if (that.data.store_identity == true){
				store_identity = 1
			} else if (that.data.store_identity == false){//定制商家身份
				store_identity = 2
			}
			app.util.request({
				url: 'entry/wxapp/Pripublish',
				data: {
					openid: wx.getStorageSync('openid'),
					title: that.data.title,
					store_identity: store_identity,
					content: that.data.content,
					img: e.join(","),
					c_id: that.data.nav_id,
					formid: that.data.formid
				},
				//发布方式（为您我要、信用、发布费、推荐）：1、0[0]0x0；2、0[0]0x1;3、0[1](x0)x0；4、0[1](x0)x1；5、0[1](x1)x0；6、0[1](x1)x1；7、0[2](x0)x0；8、0[2](x0)x1；9、0[2](x1)x0；10、0[2](x1)x1；11、1(00)00；12、1(00)01；13、1(00)10；14、1(00)11；15、1(01)00；16、1(01)01；17、1(10)10；18、1(10)11；19、1(11)00；20、1(11)01；21、1(11)10；22、1(11)11；23、1(01)10；24、1(01)11；25、1(10)00；26、1(10)01；27、1000；28、1001；29、1010；30、1011；
				success: function (res) {
					if(res.data.code==200){
						if (!that.data.store_identity) {//为您定制
							if (that.data.total > 0) {
								if (that.data.upstate == 0 && that.data.crest == 0 && that.data.crestup == 0) {
									if (that.data.cost.publishfee > 0) {//13
										app.util.request({
											url: 'entry/wxapp/Pripay',
											data: {
												openid: wx.getStorageSync('openid'),
												publishfee: that.data.cost.publishfee,
												upfee: that.data.cost.upfee,
												upstate: that.data.upstate,
												res_id: res.data.res_id
											},
											success: function (pres) {
												wx.requestPayment({
													'timeStamp': pres.data.timeStamp,
													'nonceStr': pres.data.nonceStr,
													'package': pres.data.package,
													'signType': 'MD5',
													'paySign': pres.data.paySign,
													'success': function (ores) {
														app.util.request({
															url: 'entry/wxapp/Prisuccess',
															data: {
																upstate: that.data.upstate,
																res_id: res.data.res_id
															},
															success: function (sres) {
																if (sres.data.code == 200) {
																	wx.showModal({
																		title: '提示',
																		content: '你的定制已经成功发布，可在我的定制中查看审核状态',
																		success: function (qres) {
																			if (qres.confirm) {
																				wx.redirectTo({
																					url: '../dingzhi/my',
																				})
																			} else if (qres.cancel) {
																				wx.navigateBack({
																					delta: 1
																				})
																			}
																		}
																	})
																} else {
																	wx.showToast({
																		title: '发布出错！',
																		duration: 3000
																	})
																}
															}
														})
													},
													'fail': function (res) {
														wx.showToast({
															title: '支付出错！',
															duration: 3000
														})
													}
												})
											}
										})
									} else {//11
										app.util.request({
											url: 'entry/wxapp/Prisuccess',
											data: {
												upstate: that.data.upstate,
												res_id: res.data.res_id
											},
											success: function (sres) {
												if (sres.data.code == 200) {
													wx.showModal({
														title: '提示',
														content: '你的定制已经成功发布，可在我的定制中查看审核状态',
														success: function (qres) {
															if (qres.confirm) {
																wx.redirectTo({
																	url: '../dingzhi/my',
																})
															} else if (qres.cancel) {
																wx.navigateBack({
																	delta: 1
																})
															}
														}
													})
												} else {
													wx.showToast({
														title: '发布出错！',
														duration: 3000
													})
												}
											}
										})
									}
								} else if (that.data.upstate == 1 && that.data.crest == 0 && that.data.crestup == 0) {
									if (that.data.cost.publishfee > 0) {//14
										app.util.request({
											url: 'entry/wxapp/Pripay',
											data: {
												openid: wx.getStorageSync('openid'),
												publishfee: that.data.cost.publishfee,
												upfee: that.data.cost.upfee,
												upstate: that.data.upstate,
												res_id: res.data.res_id
											},
											success: function (pres) {
												wx.requestPayment({
													'timeStamp': pres.data.timeStamp,
													'nonceStr': pres.data.nonceStr,
													'package': pres.data.package,
													'signType': 'MD5',
													'paySign': pres.data.paySign,
													'success': function (ores) {
														app.util.request({
															url: 'entry/wxapp/Prisuccess',
															data: {
																upstate: that.data.upstate,
																res_id: res.data.res_id
															},
															success: function (sres) {
																if (sres.data.code == 200) {
																	wx.showModal({
																		title: '提示',
																		content: '你的定制已经成功发布，可在我的定制中查看审核状态',
																		success: function (qres) {
																			if (qres.confirm) {
																				wx.redirectTo({
																					url: '../dingzhi/my',
																				})
																			} else if (qres.cancel) {
																				wx.navigateBack({
																					delta: 1
																				})
																			}
																		}
																	})
																} else {
																	wx.showToast({
																		title: '发布出错！',
																		duration: 3000
																	})
																}
															}
														})
													},
													'fail': function (res) {
														wx.showToast({
															title: '支付出错！',
															duration: 3000
														})
													}
												})
											}
										})

									} else {//12
										if (that.data.cost.upfee > 0) {
											app.util.request({
												url: 'entry/wxapp/Pripay',
												data: {
													openid: wx.getStorageSync('openid'),
													publishfee: that.data.cost.publishfee,
													upfee: that.data.cost.upfee,
													upstate: that.data.upstate,
													res_id: res.data.res_id
												},
												success: function (pres) {
													wx.requestPayment({
														'timeStamp': pres.data.timeStamp,
														'nonceStr': pres.data.nonceStr,
														'package': pres.data.package,
														'signType': 'MD5',
														'paySign': pres.data.paySign,
														'success': function (ores) {
															app.util.request({
																url: 'entry/wxapp/Prisuccess',
																data: {
																	upstate: that.data.upstate,
																	res_id: res.data.res_id
																},
																success: function (sres) {
																	if (sres.data.code == 200) {
																		wx.showModal({
																			title: '提示',
																			content: '你的定制已经成功发布，可在我的定制中查看审核状态',
																			success: function (qres) {
																				if (qres.confirm) {
																					wx.redirectTo({
																						url: '../dingzhi/my',
																					})
																				} else if (qres.cancel) {
																					wx.navigateBack({
																						delta: 1
																					})
																				}
																			}
																		})
																	} else {
																		wx.showToast({
																			title: '发布出错！',
																			duration: 3000
																		})
																	}
																}
															})
														},
														'fail': function (res) {
															wx.showToast({
																title: '支付出错！',
																duration: 3000
															})
														}
													})
												}
											})
										}else{
											app.util.request({
												url: 'entry/wxapp/Prisuccess',
												data: {
													upstate: that.data.upstate,
													res_id: res.data.res_id
												},
												success: function (sres) {
													if (sres.data.code == 200) {
														wx.showModal({
															title: '提示',
															content: '你的定制已经成功发布，可在我的定制中查看审核状态',
															success: function (qres) {
																if (qres.confirm) {
																	wx.redirectTo({
																		url: '../dingzhi/my',
																	})
																} else if (qres.cancel) {
																	wx.navigateBack({
																		delta: 1
																	})
																}
															}
														})
													} else {
														wx.showToast({
															title: '发布出错！',
															duration: 3000
														})
													}
												}
											})
										}

									}
								} else if (that.data.upstate == 0 && that.data.crest == 0 && that.data.crestup > 0){
									if(that.data.cost.publishfee>0){//23
										app.util.request({
											url: 'entry/wxapp/Pripay',
											data: {
												openid: wx.getStorageSync('openid'),
												publishfee: that.data.cost.publishfee,
												upfee: that.data.cost.upfee,
												upstate: that.data.upstate,
												res_id: res.data.res_id
											},
											success: function (pres) {
												wx.requestPayment({
													'timeStamp': pres.data.timeStamp,
													'nonceStr': pres.data.nonceStr,
													'package': pres.data.package,
													'signType': 'MD5',
													'paySign': pres.data.paySign,
													'success': function (ores) {
														app.util.request({
															url: 'entry/wxapp/Prisuccess',
															data: {
																upstate: that.data.upstate,
																res_id: res.data.res_id
															},
															success: function (sres) {
																if (sres.data.code == 200) {
																	wx.showModal({
																		title: '提示',
																		content: '你的定制已经成功发布，可在我的定制中查看审核状态',
																		success: function (qres) {
																			if (qres.confirm) {
																				wx.redirectTo({
																					url: '../dingzhi/my',
																				})
																			} else if (qres.cancel) {
																				wx.navigateBack({
																					delta: 1
																				})
																			}
																		}
																	})
																} else {
																	wx.showToast({
																		title: '发布出错！',
																		duration: 3000
																	})
																}
															}
														})
													},
													'fail': function (res) {
														wx.showToast({
															title: '支付出错！',
															duration: 3000
														})
													}
												})
											}
										})
									}else{//15
										app.util.request({
											url: 'entry/wxapp/Prisuccess',
											data: {
												upstate: that.data.upstate,
												res_id: res.data.res_id
											},
											success: function (sres) {
												if (sres.data.code == 200) {
													wx.showModal({
														title: '提示',
														content: '你的定制已经成功发布，可在我的定制中查看审核状态',
														success: function (qres) {
															if (qres.confirm) {
																wx.redirectTo({
																	url: '../dingzhi/my',
																})
															} else if (qres.cancel) {
																wx.navigateBack({
																	delta: 1
																})
															}
														}
													})
												} else {
													wx.showToast({
														title: '发布出错！',
														duration: 3000
													})
												}
											}
										})
									}
								} else if (that.data.upstate == 1 && that.data.crest == 0 && that.data.crestup > 0){
									if (that.data.cost.publishfee > 0){//24
										app.util.request({
											url: 'entry/wxapp/Pripay',
											data: {
												openid: wx.getStorageSync('openid'),
												publishfee: that.data.cost.publishfee,
												upfee: that.data.cost.upfee,
												upstate: that.data.upstate,
												res_id: res.data.res_id,
												alltype:24
											},
											success: function (pres) {
												wx.requestPayment({
													'timeStamp': pres.data.timeStamp,
													'nonceStr': pres.data.nonceStr,
													'package': pres.data.package,
													'signType': 'MD5',
													'paySign': pres.data.paySign,
													'success': function (ores) {
														app.util.request({
															url: 'entry/wxapp/Prisuccess',
															data: {
																upstate: that.data.upstate,
																res_id: res.data.res_id,
																openid: wx.getStorageSync('openid'),
																alltype: 24
															},
															success: function (sres) {
																if (sres.data.code == 200) {
																	wx.showModal({
																		title: '提示',
																		content: '你的定制已经成功发布，可在我的定制中查看审核状态',
																		success: function (qres) {
																			if (qres.confirm) {
																				wx.redirectTo({
																					url: '../dingzhi/my',
																				})
																			} else if (qres.cancel) {
																				wx.navigateBack({
																					delta: 1
																				})
																			}
																		}
																	})
																} else {
																	wx.showToast({
																		title: '发布出错！',
																		duration: 3000
																	})
																}
															}
														})
													},
													'fail': function (res) {
														wx.showToast({
															title: '支付出错哟！',
															duration: 3000
														})
													}
												})
											}
										})
									}else{//16
										app.util.request({
											url: 'entry/wxapp/Prisuccess',
											data: {
												upstate: that.data.upstate,
												openid: wx.getStorageSync('openid'),
												res_id: res.data.res_id,
												alltype:16
											},
											success: function (sres) {
												if (sres.data.code == 200) {
													wx.showModal({
														title: '提示',
														content: '你的定制已经成功发布，可在我的定制中查看审核状态',
														success: function (qres) {
															if (qres.confirm) {
																wx.redirectTo({
																	url: '../dingzhi/my',
																})
															} else if (qres.cancel) {
																wx.navigateBack({
																	delta: 1
																})
															}
														}
													})
												} else {
													wx.showToast({
														title: '发布出错！',
														duration: 3000
													})
												}
											}
										})
									}
								} else if (that.data.upstate == 0 && that.data.crest > 0 && that.data.crestup == 0){
									if(that.data.cost.publishfee>0){//17
										app.util.request({
											url: 'entry/wxapp/Prisuccess',
											data: {
												upstate: that.data.upstate,
												res_id: res.data.res_id,
												openid: wx.getStorageSync('openid'),
												alltype:17
											},
											success: function (sres) {
												if (sres.data.code == 200) {
													wx.showModal({
														title: '提示',
														content: '你的定制已经成功发布，可在我的定制中查看审核状态',
														success: function (qres) {
															if (qres.confirm) {
																wx.redirectTo({
																	url: '../dingzhi/my',
																})
															} else if (qres.cancel) {
																wx.navigateBack({
																	delta: 1
																})
															}
														}
													})
												} else {
													wx.showToast({
														title: '发布出错！',
														duration: 3000
													})
												}
											}
										})
									}else{//25
										app.util.request({
											url: 'entry/wxapp/Prisuccess',
											data: {
												upstate: that.data.upstate,
												res_id: res.data.res_id
											},
											success: function (sres) {
												if (sres.data.code == 200) {
													wx.showModal({
														title: '提示',
														content: '你的定制已经成功发布，可在我的定制中查看审核状态',
														success: function (qres) {
															if (qres.confirm) {
																wx.redirectTo({
																	url: '../dingzhi/my',
																})
															} else if (qres.cancel) {
																wx.navigateBack({
																	delta: 1
																})
															}
														}
													})
												} else {
													wx.showToast({
														title: '发布出错！',
														duration: 3000
													})
												}
											}
										})
									}
								} else if (that.data.upstate == 1 && that.data.crest > 0 && that.data.crestup == 0) {
									if (that.data.cost.publishfee > 0) {//18
										app.util.request({
											url: 'entry/wxapp/Pripay',
											data: {
												openid: wx.getStorageSync('openid'),
												publishfee: that.data.cost.publishfee,
												upfee: that.data.cost.upfee,
												upstate: that.data.upstate,
												res_id: res.data.res_id,
												alltype:18
											},
											success: function (pres) {
												wx.requestPayment({
													'timeStamp': pres.data.timeStamp,
													'nonceStr': pres.data.nonceStr,
													'package': pres.data.package,
													'signType': 'MD5',
													'paySign': pres.data.paySign,
													'success': function (ores) {
														app.util.request({
															url: 'entry/wxapp/Prisuccess',
															data: {
																upstate: that.data.upstate,
																openid: wx.getStorageSync('openid'),
																res_id: res.data.res_id,
																alltype:18
															},
															success: function (sres) {
																if (sres.data.code == 200) {
																	wx.showModal({
																		title: '提示',
																		content: '你的定制已经成功发布，可在我的定制中查看审核状态',
																		success: function (qres) {
																			if (qres.confirm) {
																				wx.redirectTo({
																					url: '../dingzhi/my',
																				})
																			} else if (qres.cancel) {
																				wx.navigateBack({
																					delta: 1
																				})
																			}
																		}
																	})
																} else {
																	wx.showToast({
																		title: '发布出错！',
																		duration: 3000
																	})
																}
															}
														})
													},
													'fail': function (res) {
														wx.showToast({
															title: '支付出错！',
															duration: 3000
														})
													}
												})
											}
										})
									} else {//26
										if(that.data.cost.upfee>0){
											app.util.request({
												url: 'entry/wxapp/Pripay',
												data: {
													openid: wx.getStorageSync('openid'),
													publishfee: that.data.cost.publishfee,
													upfee: that.data.cost.upfee,
													upstate: that.data.upstate,
													res_id: res.data.res_id
												},
												success: function (pres) {
													wx.requestPayment({
														'timeStamp': pres.data.timeStamp,
														'nonceStr': pres.data.nonceStr,
														'package': pres.data.package,
														'signType': 'MD5',
														'paySign': pres.data.paySign,
														'success': function (ores) {
															app.util.request({
																url: 'entry/wxapp/Prisuccess',
																data: {
																	upstate: that.data.upstate,
																	res_id: res.data.res_id
																},
																success: function (sres) {
																	if (sres.data.code == 200) {
																		wx.showModal({
																			title: '提示',
																			content: '你的定制已经成功发布，可在我的定制中查看审核状态',
																			success: function (qres) {
																				if (qres.confirm) {
																					wx.redirectTo({
																						url: '../dingzhi/my',
																					})
																				} else if (qres.cancel) {
																					wx.navigateBack({
																						delta: 1
																					})
																				}
																			}
																		})
																	} else {
																		wx.showToast({
																			title: '发布出错！',
																			duration: 3000
																		})
																	}
																}
															})
														},
														'fail': function (res) {
															wx.showToast({
																title: '支付出错！',
																duration: 3000
															})
														}
													})
												}
											})
										}else{
											app.util.request({
												url: 'entry/wxapp/Prisuccess',
												data: {
													upstate: that.data.upstate,
													res_id: res.data.res_id
												},
												success: function (sres) {
													if (sres.data.code == 200) {
														wx.showModal({
															title: '提示',
															content: '你的定制已经成功发布，可在我的定制中查看审核状态',
															success: function (qres) {
																if (qres.confirm) {
																	wx.redirectTo({
																		url: '../dingzhi/my',
																	})
																} else if (qres.cancel) {
																	wx.navigateBack({
																		delta: 1
																	})
																}
															}
														})
													} else {
														wx.showToast({
															title: '发布出错！',
															duration: 3000
														})
													}
												}
											})
										}
									}
								} else if (that.data.upstate == 0 && that.data.crest > 0 && that.data.crestup > 0){
									if(that.data.cost.publishfee>0){//21
										app.util.request({
											url: 'entry/wxapp/Prisuccess',
											data: {
												upstate: that.data.upstate,
												openid: wx.getStorageSync('openid'),
												res_id: res.data.res_id,
												alltype: 21
											},
											success: function (sres) {
												if (sres.data.code == 200) {
													wx.showModal({
														title: '提示',
														content: '你的定制已经成功发布，可在我的定制中查看审核状态',
														success: function (qres) {
															if (qres.confirm) {
																wx.redirectTo({
																	url: '../dingzhi/my',
																})
															} else if (qres.cancel) {
																wx.navigateBack({
																	delta: 1
																})
															}
														}
													})
												} else {
													wx.showToast({
														title: '发布出错！',
														duration: 3000
													})
												}
											}
										})
									}else{//19
										app.util.request({
											url: 'entry/wxapp/Prisuccess',
											data: {
												upstate: that.data.upstate,
												openid: wx.getStorageSync('openid'),
												res_id: res.data.res_id,
												alltype: 19
											},
											success: function (sres) {
												if (sres.data.code == 200) {
													wx.showModal({
														title: '提示',
														content: '你的定制已经成功发布，可在我的定制中查看审核状态',
														success: function (qres) {
															if (qres.confirm) {
																wx.redirectTo({
																	url: '../dingzhi/my',
																})
															} else if (qres.cancel) {
																wx.navigateBack({
																	delta: 1
																})
															}
														}
													})
												} else {
													wx.showToast({
														title: '发布出错！',
														duration: 3000
													})
												}
											}
										})
									}
								} else if (that.data.upstate == 1 && that.data.crest > 0 && that.data.crestup > 0) {
									if (that.data.cost.publishfee > 0) {//22
										app.util.request({
											url: 'entry/wxapp/Prisuccess',
											data: {
												upstate: that.data.upstate,
												openid: wx.getStorageSync('openid'),
												res_id: res.data.res_id,
												alltype: 22
											},
											success: function (sres) {
												if (sres.data.code == 200) {
													wx.showModal({
														title: '提示',
														content: '你的定制已经成功发布，可在我的定制中查看审核状态',
														success: function (qres) {
															if (qres.confirm) {
																wx.redirectTo({
																	url: '../dingzhi/my',
																})
															} else if (qres.cancel) {
																wx.navigateBack({
																	delta: 1
																})
															}
														}
													})
												} else {
													wx.showToast({
														title: '发布出错！',
														duration: 3000
													})
												}
											}
										})
									} else {//20
										app.util.request({
											url: 'entry/wxapp/Prisuccess',
											data: {
												upstate: that.data.upstate,
												openid: wx.getStorageSync('openid'),
												res_id: res.data.res_id,
												alltype: 20
											},
											success: function (sres) {
												if (sres.data.code == 200) {
													wx.showModal({
														title: '提示',
														content: '你的定制已经成功发布，可在我的定制中查看审核状态',
														success: function (qres) {
															if (qres.confirm) {
																wx.redirectTo({
																	url: '../dingzhi/my',
																})
															} else if (qres.cancel) {
																wx.navigateBack({
																	delta: 1
																})
															}
														}
													})
												} else {
													wx.showToast({
														title: '发布出错！',
														duration: 3000
													})
												}
											}
										})
									}
								}
							}else{
								if(that.data.upstate==0){
									if(that.data.cost.publishfee>0){
										app.util.request({
											url: 'entry/wxapp/Pripay',
											data: {
												openid: wx.getStorageSync('openid'),
												publishfee: that.data.cost.publishfee,
												upfee: that.data.cost.upfee,
												upstate: that.data.upstate,
												res_id: res.data.res_id
											},
											success: function (pres) {
												wx.requestPayment({
													'timeStamp': pres.data.timeStamp,
													'nonceStr': pres.data.nonceStr,
													'package': pres.data.package,
													'signType': 'MD5',
													'paySign': pres.data.paySign,
													'success': function (ores) {
														app.util.request({
															url: 'entry/wxapp/Prisuccess',
															data: {
																upstate: that.data.upstate,
																res_id: res.data.res_id
															},
															success: function (sres) {
																if (sres.data.code == 200) {
																	wx.showModal({
																		title: '提示',
																		content: '你的定制已经成功发布，可在我的定制中查看审核状态',
																		success: function (qres) {
																			if (qres.confirm) {
																				wx.redirectTo({
																					url: '../dingzhi/my',
																				})
																			} else if (qres.cancel) {
																				wx.navigateBack({
																					delta: 1
																				})
																			}
																		}
																	})
																} else {
																	wx.showToast({
																		title: '发布出错！',
																		duration: 3000
																	})
																}
															}
														})
													},
													'fail': function (res) {
														wx.showToast({
															title: '支付出错！',
															duration: 3000
														})
													}
												})
											}
										})
									}else{//27
										app.util.request({
											url: 'entry/wxapp/Prisuccess',
											data: {
												upstate: that.data.upstate,
												res_id: res.data.res_id,
											},
											success: function (sres) {
												if (sres.data.code == 200) {
													wx.showModal({
														title: '提示',
														content: '你的定制已经成功发布，可在我的定制中查看审核状态',
														success: function (qres) {
															if (qres.confirm) {
																wx.redirectTo({
																	url: '../dingzhi/my',
																})
															} else if (qres.cancel) {
																wx.navigateBack({
																	delta: 1
																})
															}
														}
													})
												} else {
													wx.showToast({
														title: '发布出错！',
														duration: 3000
													})
												}
											}
										})
									}
								} else if (that.data.upstate == 1) {
									if (that.data.cost.publishfee > 0) {
										app.util.request({
											url: 'entry/wxapp/Pripay',
											data: {
												openid: wx.getStorageSync('openid'),
												publishfee: that.data.cost.publishfee,
												upfee: that.data.cost.upfee,
												upstate: that.data.upstate,
												res_id: res.data.res_id
											},
											success: function (pres) {
												wx.requestPayment({
													'timeStamp': pres.data.timeStamp,
													'nonceStr': pres.data.nonceStr,
													'package': pres.data.package,
													'signType': 'MD5',
													'paySign': pres.data.paySign,
													'success': function (ores) {
														app.util.request({
															url: 'entry/wxapp/Prisuccess',
															data: {
																upstate: that.data.upstate,
																res_id: res.data.res_id
															},
															success: function (sres) {
																if (sres.data.code == 200) {
																	wx.showModal({
																		title: '提示',
																		content: '你的定制已经成功发布，可在我的定制中查看审核状态',
																		success: function (qres) {
																			if (qres.confirm) {
																				wx.redirectTo({
																					url: '../dingzhi/my',
																				})
																			} else if (qres.cancel) {
																				wx.navigateBack({
																					delta: 1
																				})
																			}
																		}
																	})
																} else {
																	wx.showToast({
																		title: '发布出错！',
																		duration: 3000
																	})
																}
															}
														})
													},
													'fail': function (res) {
														wx.showToast({
															title: '支付出错！',
															duration: 3000
														})
													}
												})
											}
										})
									} else {//28
										if(that.data.cost.upfee>0){
											app.util.request({
												url: 'entry/wxapp/Pripay',
												data: {
													openid: wx.getStorageSync('openid'),
													publishfee: that.data.cost.publishfee,
													upfee: that.data.cost.upfee,
													upstate: that.data.upstate,
													res_id: res.data.res_id
												},
												success: function (pres) {
													wx.requestPayment({
														'timeStamp': pres.data.timeStamp,
														'nonceStr': pres.data.nonceStr,
														'package': pres.data.package,
														'signType': 'MD5',
														'paySign': pres.data.paySign,
														'success': function (ores) {
															app.util.request({
																url: 'entry/wxapp/Prisuccess',
																data: {
																	upstate: that.data.upstate,
																	res_id: res.data.res_id
																},
																success: function (sres) {
																	if (sres.data.code == 200) {
																		wx.showModal({
																			title: '提示',
																			content: '你的定制已经成功发布，可在我的定制中查看审核状态',
																			success: function (qres) {
																				if (qres.confirm) {
																					wx.redirectTo({
																						url: '../dingzhi/my',
																					})
																				} else if (qres.cancel) {
																					wx.navigateBack({
																						delta: 1
																					})
																				}
																			}
																		})
																	} else {
																		wx.showToast({
																			title: '发布出错！',
																			duration: 3000
																		})
																	}
																}
															})
														},
														'fail': function (res) {
															wx.showToast({
																title: '支付出错！',
																duration: 3000
															})
														}
													})
												}
											})
										}else{
											app.util.request({
												url: 'entry/wxapp/Prisuccess',
												data: {
													upstate: that.data.upstate,
													res_id: res.data.res_id
												},
												success: function (sres) {
													if (sres.data.code == 200) {
														wx.showModal({
															title: '提示',
															content: '你的定制已经成功发布，可在我的定制中查看审核状态',
															success: function (qres) {
																if (qres.confirm) {
																	wx.redirectTo({
																		url: '../dingzhi/my',
																	})
																} else if (qres.cancel) {
																	wx.navigateBack({
																		delta: 1
																	})
																}
															}
														})
													} else {
														wx.showToast({
															title: '发布出错！',
															duration: 3000
														})
													}
												}
											})
										}
									}
								}
							}
						} else {//我要定制
							if (that.data.useriden == 0) {//用户身份
								if (that.data.upstate == 0) {
									app.util.request({//1
										url: 'entry/wxapp/Prisuccess',
										data: {
											upstate: that.data.upstate,
											res_id: res.data.res_id
										},
										success: function (sres) {
											if (sres.data.code == 200) {
												wx.showModal({
													title: '提示',
													content: '你的定制已经成功发布，可在我的定制中查看审核状态',
													success: function (qres) {
														if (qres.confirm) {
															wx.redirectTo({
																url: '../dingzhi/my',
															})
														} else if (qres.cancel) {
															wx.navigateBack({
																delta: 1
															})
														}
													}
												})
											}else{
												wx.showToast({
													title: '发布出错！',
													duration: 3000
												})
											}
										}
									})
								} else if (that.data.upstate == 1) {//2
									if(that.data.cost.upfee>0){
										app.util.request({
											url: 'entry/wxapp/Pripay',
											data: {
												openid: wx.getStorageSync('openid'),
												publishfee: that.data.cost.publishfee,
												upfee: that.data.cost.upfee,
												upstate: that.data.upstate,
												res_id: res.data.res_id
											},
											success: function (pres) {
												wx.requestPayment({
													'timeStamp': pres.data.timeStamp,
													'nonceStr': pres.data.nonceStr,
													'package': pres.data.package,
													'signType': 'MD5',
													'paySign': pres.data.paySign,
													'success': function (ores) {
														app.util.request({//2
															url: 'entry/wxapp/Prisuccess',
															data: {
																upstate: that.data.upstate,
																res_id: res.data.res_id
															},
															success: function (sres) {
																if (sres.data.code == 200) {
																	wx.showModal({
																		title: '提示',
																		content: '你的定制已经成功发布，可在我的定制中查看审核状态',
																		success: function (qres) {
																			if (qres.confirm) {
																				wx.redirectTo({
																					url: '../dingzhi/my',
																				})
																			} else if (qres.cancel) {
																				wx.navigateBack({
																					delta: 1
																				})
																			}
																		}
																	})
																} else {
																	wx.showToast({
																		title: '发布出错！',
																		duration: 3000
																	})
																}
															}
														})
													},
													'fail': function (res) {
														wx.showToast({
															title: '支付出错！',
															duration: 3000
														})
													}
												})
											}
										})
									}else{
										app.util.request({//2
											url: 'entry/wxapp/Prisuccess',
											data: {
												upstate: that.data.upstate,
												res_id: res.data.res_id
											},
											success: function (sres) {
												if (sres.data.code == 200) {
													wx.showModal({
														title: '提示',
														content: '你的定制已经成功发布，可在我的定制中查看审核状态',
														success: function (qres) {
															if (qres.confirm) {
																wx.redirectTo({
																	url: '../dingzhi/my',
																})
															} else if (qres.cancel) {
																wx.navigateBack({
																	delta: 1
																})
															}
														}
													})
												} else {
													wx.showToast({
														title: '发布出错！',
														duration: 3000
													})
												}
											}
										})
									}
								}
							}
							if (that.data.useriden == 1) {//普通商家
								if(that.data.upstate==0){//3、5
									app.util.request({
										url: 'entry/wxapp/Prisuccess',
										data: {
											upstate: that.data.upstate,
											res_id: res.data.res_id
										},
										success: function (sres) {
											if (sres.data.code == 200) {
												wx.showModal({
													title: '提示',
													content: '你的定制已经成功发布，可在我的定制中查看审核状态',
													success: function (qres) {
														if (qres.confirm) {
															wx.redirectTo({
																url: '../dingzhi/my',
															})
														} else if (qres.cancel) {
															wx.navigateBack({
																delta: 1
															})
														}
													}
												})
											} else {
												wx.showToast({
													title: '发布出错！',
													duration: 3000
												})
											}
										}
									})
								}else if(that.data.upstate==1){
									if (that.data.urestup > 0) {//6
										app.util.request({
											url: 'entry/wxapp/Prisuccess',
											data: {
												upstate: that.data.upstate,
												openid: wx.getStorageSync('openid'),
												res_id: res.data.res_id,
												alltype:6
											},
											success: function (sres) {
												if (sres.data.code == 200) {
													wx.showModal({
														title: '提示',
														content: '你的定制已经成功发布，可在我的定制中查看审核状态',
														success: function (qres) {
															if (qres.confirm) {
																wx.redirectTo({
																	url: '../dingzhi/my',
																})
															} else if (qres.cancel) {
																wx.navigateBack({
																	delta: 1
																})
															}
														}
													})
												} else {
													wx.showToast({
														title: '发布出错！',
														duration: 3000
													})
												}
											}
										})
									} else {//4
										if(that.data.cost.upfee>0){
											app.util.request({
												url: 'entry/wxapp/Pripay',
												data: {
													openid: wx.getStorageSync('openid'),
													publishfee: that.data.cost.publishfee,
													upfee: that.data.cost.upfee,
													upstate: that.data.upstate,
													res_id: res.data.res_id
												},
												success: function (pres) {
													wx.requestPayment({
														'timeStamp': pres.data.timeStamp,
														'nonceStr': pres.data.nonceStr,
														'package': pres.data.package,
														'signType': 'MD5',
														'paySign': pres.data.paySign,
														'success': function (ores) {
															app.util.request({
																url: 'entry/wxapp/Prisuccess',
																data: {
																	upstate: that.data.upstate,
																	res_id: res.data.res_id
																},
																success: function (sres) {
																	if (sres.data.code == 200) {
																		wx.showModal({
																			title: '提示',
																			content: '你的定制已经成功发布，可在我的定制中查看审核状态',
																			success: function (qres) {
																				if (qres.confirm) {
																					wx.redirectTo({
																						url: '../dingzhi/my',
																					})
																				} else if (qres.cancel) {
																					wx.navigateBack({
																						delta: 1
																					})
																				}
																			}
																		})
																	} else {
																		wx.showToast({
																			title: '发布出错！',
																			duration: 3000
																		})
																	}
																}
															})
														},
														'fail': function (res) {
															wx.showToast({
																title: '支付出错！',
																duration: 3000
															})
														}
													})
												}
											})
										}else{
											app.util.request({
												url: 'entry/wxapp/Prisuccess',
												data: {
													upstate: that.data.upstate,
													res_id: res.data.res_id
												},
												success: function (sres) {
													if (sres.data.code == 200) {
														wx.showModal({
															title: '提示',
															content: '你的定制已经成功发布，可在我的定制中查看审核状态',
															success: function (qres) {
																if (qres.confirm) {
																	wx.redirectTo({
																		url: '../dingzhi/my',
																	})
																} else if (qres.cancel) {
																	wx.navigateBack({
																		delta: 1
																	})
																}
															}
														})
													} else {
														wx.showToast({
															title: '发布出错！',
															duration: 3000
														})
													}
												}
											})
										}
									}
								}
							}
							if (that.data.useriden == 2) {//定制商家
								if (that.data.upstate == 0) {//7、9
									app.util.request({
										url: 'entry/wxapp/Prisuccess',
										data: {
											upstate: that.data.upstate,
											res_id: res.data.res_id
										},
										success: function (sres) {
											if (sres.data.code == 200) {
												wx.showModal({
													title: '提示',
													content: '你的定制已经成功发布，可在我的定制中查看审核状态',
													success: function (qres) {
														if (qres.confirm) {
															wx.redirectTo({
																url: '../dingzhi/my',
															})
														} else if (qres.cancel) {
															wx.navigateBack({
																delta: 1
															})
														}
													}
												})
											} else {
												wx.showToast({
													title: '发布出错！',
													duration: 3000
												})
											}
										}
									})
								} else if (that.data.upstate == 1) {
									if (that.data.urestup > 0) {//10
										app.util.request({
											url: 'entry/wxapp/Prisuccess',
											data: {
												upstate: that.data.upstate,
												openid: wx.getStorageSync('openid'),
												res_id: res.data.res_id,
												alltype: 10
											},
											success: function (sres) {
												if (sres.data.code == 200) {
													wx.showModal({
														title: '提示',
														content: '你的定制已经成功发布，可在我的定制中查看审核状态',
														success: function (qres) {
															if (qres.confirm) {
																wx.redirectTo({
																	url: '../dingzhi/my',
																})
															} else if (qres.cancel) {
																wx.navigateBack({
																	delta: 1
																})
															}
														}
													})
												} else {
													wx.showToast({
														title: '发布出错！',
														duration: 3000
													})
												}
											}
										})
									} else {//8
										if (that.data.cost.upfee > 0) {
											app.util.request({
												url: 'entry/wxapp/Pripay',
												data: {
													openid: wx.getStorageSync('openid'),
													publishfee: that.data.cost.publishfee,
													upfee: that.data.cost.upfee,
													upstate: that.data.upstate,
													res_id: res.data.res_id
												},
												success: function (pres) {
													wx.requestPayment({
														'timeStamp': pres.data.timeStamp,
														'nonceStr': pres.data.nonceStr,
														'package': pres.data.package,
														'signType': 'MD5',
														'paySign': pres.data.paySign,
														'success': function (ores) {
															app.util.request({
																url: 'entry/wxapp/Prisuccess',
																data: {
																	upstate: that.data.upstate,
																	res_id: res.data.res_id
																},
																success: function (sres) {
																	if (sres.data.code == 200) {
																		wx.showModal({
																			title: '提示',
																			content: '你的定制已经成功发布，可在我的定制中查看审核状态',
																			success: function (qres) {
																				if (qres.confirm) {
																					wx.redirectTo({
																						url: '../dingzhi/my',
																					})
																				} else if (qres.cancel) {
																					wx.navigateBack({
																						delta: 1
																					})
																				}
																			}
																		})
																	} else {
																		wx.showToast({
																			title: '发布出错！',
																			duration: 3000
																		})
																	}
																}
															})
														},
														'fail': function (res) {
															wx.showToast({
																title: '支付出错！',
																duration: 3000
															})
														}
													})
												}
											})
										}else{
											app.util.request({
												url: 'entry/wxapp/Prisuccess',
												data: {
													upstate: that.data.upstate,
													res_id: res.data.res_id
												},
												success: function (sres) {
													if (sres.data.code == 200) {
														wx.showModal({
															title: '提示',
															content: '你的定制已经成功发布，可在我的定制中查看审核状态',
															success: function (qres) {
																if (qres.confirm) {
																	wx.redirectTo({
																		url: '../dingzhi/my',
																	})
																} else if (qres.cancel) {
																	wx.navigateBack({
																		delta: 1
																	})
																}
															}
														})
													} else {
														wx.showToast({
															title: '发布出错！',
															duration: 3000
														})
													}
												}
											})
										}
									}
								}
							}
						}
					}else{
						wx.showToast({
							title: '发布出错',
							duration:3000
						})
					}
				}
				// success: res => {
				// 	var release
				// 	var recommed
				// 	var money 
				// 	if (!that.data.store_identity && that.data.checked) {
				// 		release = Number(that.data.cost.publishfee)
				// 		recommed = Number(that.data.cost.upfee)
				// 		money = release + recommed
				// 	}
				// 	if (!that.data.store_identity && !that.data.checked) {
				// 		release = Number(that.data.cost.publishfee)
				// 		recommed = 0
				// 		money = release + recommed
				// 	}
				// 	if (that.data.store_identity && that.data.checked) {
				// 		release = 0
				// 		recommed = Number(that.data.cost.upfee)
				// 		money = release + recommed
				// 	}
				// 	if (that.data.store_identity && !that.data.checked) {
				// 		release =0
				// 		recommed = 0
				// 		money = release + recommed
				// 	} 
				// 	// 提交成功之后根据需要付款的值大小判断调用哪个接口
				// 	if (money > 0) {
				// 		// 调用支付
				// 		that.pay(release, recommed, that.data.upstate, res.data.res_id)
				// 	} else {
				// 		// 跳过支付直接修改订单状态
				// 		that.change_status(that.data.upstate, res.data.res_id)
				// 	}
				// }
			})
  },
  pay: function(release, recommed, upstate, res_id) {
    var that = this
    app.util.request({
      url: 'entry/wxapp/Pripay',
      data: {
				openid: wx.getStorageSync('openid'),
        publishfee: release,
        upfee: recommed,
        upstate: upstate,
        res_id: res_id
      },
      success: res => {
        
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            that.change_status(upstate,res_id)

            wx.showModal({
              title: '提示',
              content: '您的定制发布已经提交，是否需要到我的定制中查看？',
              success: function (res) {
                if (res.confirm) {
                  
									wx.redirectTo({
                    url: '../dingzhi/my',
                  })
                } else if (res.cancel) {
                  wx.switchTab({
                    url: '../index/index',
                  })
                }
              }
            })
          },
          'fail': function (res) {
          }
        })
      }
    })
  },
  // 改变订单状态
  change_status: function(upstate, res_id) {
    var that = this,
      a = that.data
    app.util.request({
      url: 'entry/wxapp/Prisuccess',
      data: {
        upstate: upstate,
        res_id: res_id
      },
      success: res => {
        
        if(res.data.code==500){
          wx.showToast({
            title: '发布失败，请重新提交',
          })
        }else if(res.data.code==200){
          wx.showModal({
            title: '提示',
            content: '你的定制已经成功发布，可在我的定制中查看审核状态',
            success:function(res){
              if(res.confirm){
                wx.redirectTo({
                  url: '../dingzhi/my',
                })
              }else if(res.cancel){
								wx.navigateBack({
									delta: 1
								})
              }
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
		if (options.type == 2) {//我要定制
      that.setData({
				store_identity: true//我要定制、发布费隐藏
      })
			app.util.request({
				url: 'entry/wxapp/Isregister',
				data: { openid: wx.getStorageSync('openid') },
				success: function (res) {
					if (res.data.code == 200) {
						app.util.request({
							url: 'entry/wxapp/Iscustomers',
							data: { openid: wx.getStorageSync('openid') },
							success: function (cres) {
								if (res.data.code == 200) {
									that.setData({//我要定制-定制商家身份
										useriden:2
									})
								}else if (res.data.code == 204) {
									that.setData({//我要定制-普通商家身份
										useriden: 1
									})
								}else{
									that.setData({//我要定制-用户身份
										useriden: 0
									})
								}
							}
						})
					} else {
						// wx.redirectTo({
						// 	url: '../index/register',
						// })
						wx.showToast({
							title: '出现未知查询错误！',
							duration:3000
						})
					}
				}
			})
    }
    
    app.util.request({
      url: 'entry/wxapp/primoney',
      success: res => {
        
        that.setData({
          cost: res.data.res
        })
      }
    })
    app.util.request({
      url: 'entry/wxapp/catestop',
      success: res => {
        
        var nav = res.data.res
        var navs = []
        nav.map(function(item) {
          var obj = {}
          obj = item.cate
          navs.push(obj)
        })
        that.setData({
          array: navs,
          nav: nav
        })
      }
    })
    app.location_url(that)
    app.location_uniacid(that)
		getApp().util.request({
			url: 'entry/wxapp/Recostis',
			data: { openid: wx.getStorageSync('openid') },
			success: function (res) {
				if (res.data.code == 200) {
					console.log(res)
					that.setData({
						total: res.data.total
					})
				}
			}
		})
		getApp().util.request({
			url: 'entry/wxapp/Repucostforyouf',
			data: { openid: wx.getStorageSync('openid') },
			success: function (res) {
				console.log(res)
				if (res.data.code == 200) {
					that.setData({
						crest: res.data.rest,
						crestup: res.data.restup
					})
				}
			}
		})
		getApp().util.request({
			url: 'entry/wxapp/Repucostiwantf',
			data: { openid: wx.getStorageSync('openid') },
			success: function (res) {
				if (res.data.code == 200) {
					that.setData({
						urest: res.data.rest,
						urestup: res.data.restup
					})
				}
			}
		})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})